package com.wish.banking.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wish.banking.entity.FkBusinessCreditSub;
import com.wish.banking.entity.JrFactoring;
import com.wish.banking.entity.TbBusiness;
import com.wish.banking.entity.vo.FactoringVo;
import com.wish.banking.service.IFkBusinessCreditService;
import com.wish.banking.service.IJrFactoringService;
import com.wish.banking.service.ITbBusinessService;
import com.wish.common.core.usconfig.common.MsgResult;
import com.wish.common.security.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 高露雯:租约保理申请表 前端控制器
 * </p>
 *
 * @author gaoluwen
 * @since 2022-04-04
 */
@RestController
@RequestMapping("/jr-factoring")
public class JrFactoringController {
    @Autowired
    IJrFactoringService iJrFactoringService;
    @Autowired
    IFkBusinessCreditService iFkBusinessCreditService;
    @Autowired
    ITbBusinessService iTbBusinessService;
    @PostMapping("saveJrFactoring")
    public MsgResult saveJrFactoring(@RequestBody JrFactoring jrFactoring){
        MsgResult msgResult = new MsgResult();

        TbBusiness business= iTbBusinessService.getBusinessId(SecurityUtils.getUserId());
        jrFactoring.setBusinessId(business.getId().toString());
        //根据 产品id 查询是否给该商户授权了该商品
        FkBusinessCreditSub findById = iFkBusinessCreditService.findByBusinessAndProductId(jrFactoring.getJrProductId());
        
        //如果未给该商户授权该商品
        if(findById == null){
            msgResult.setMsgInfo("授信额度不足，请联系维数公寓客服：400-008-3363");
            return msgResult;
        }


        BigDecimal subtractProduct = BigDecimal.ZERO;
        System.err.println("输出bigDecimal  "+subtractProduct);
        //剩余可用授信额度 商户该产品总授信额度减去该产品已用授信额度
        if(findById.getUsedCreditline()!=null && !findById.getUsedCreditline().equals(BigDecimal.ZERO)){
            subtractProduct = findById.getTotalCreditline().subtract(findById.getUsedCreditline());
        }else{
            subtractProduct = findById.getTotalCreditline();
        }

        //传过来的申请分期金额大于改商品中的该产品剩余授信金额
        if(jrFactoring.getRentMoney().compareTo(subtractProduct)==1){
            msgResult.setMsgInfo("授信额度不足，请联系维数公寓客服：400-008-3363");
            return msgResult;
        }

        //剩余月度授信额度 商户该产品月度授信额度减去该产品月度已用授信额度
        if(findById.getMonthlyTotalCreditline()!=null && !findById.getMonthlyTotalCreditline().equals(BigDecimal.ZERO)){
            //商品总授信额度减去已用授信额度
            BigDecimal subtractMonthly = BigDecimal.ZERO;
            if(findById.getMonthlyUsedCreditline()!=null && !findById.getMonthlyUsedCreditline().equals(BigDecimal.ZERO)){
                subtractMonthly = findById.getMonthlyTotalCreditline().subtract(findById.getMonthlyUsedCreditline());
            }else{
                subtractMonthly = findById.getMonthlyTotalCreditline();
            }
            //传过来的申请分期金额大于该商户月剩余授信金额
            if(jrFactoring.getRentMoney().compareTo(subtractMonthly)==1){
                msgResult.setMsgInfo("授信额度不足，请联系维数公寓客服：400-008-3363");
                return msgResult;
            }
        }
        iJrFactoringService.saveJrFactoring(jrFactoring);
        return MsgResult.succeed();
    }

    //金融产品申请审核的列表
    @GetMapping("factoringList")
    public MsgResult factoringList(FactoringVo fv){
        PageHelper.startPage(fv.getPageNum(),fv.getPageSize());
        QueryWrapper<JrFactoring> queryWrapper = new QueryWrapper<>();
        //状态查询
        if(fv.getApplyState()!=null) {
            queryWrapper.eq("apply_state", fv.getApplyState());
        }
        //时间区间查询
        if(fv.getStartDate()!=null){
            queryWrapper.ge("create_time",fv.getStartDate());
        }
        if(fv.getEndDate()!=null){
            queryWrapper.le("create_time",fv.getEndDate());
        }
        List<JrFactoring> list = iJrFactoringService.list(queryWrapper);
        PageInfo<JrFactoring> pg = new PageInfo<>(list);
        return MsgResult.succeed(pg);
    }
    //根据id获取对象
    @PostMapping("getFactoring")
    public MsgResult getFactoring(@RequestBody JrFactoring jrFactoring){
        JrFactoring factoring = iJrFactoringService.getById(jrFactoring.getId());
        return MsgResult.succeed(factoring);
    }
    @PostMapping("updateFactoring")
    public MsgResult updateFactoring(@RequestBody JrFactoring jrFactoring){
        //1:申请中，2：初审通过，3：初审不通过，4：复审通过，5：复审不通过
        if(jrFactoring.getApplyState()==1 && jrFactoring.getYesNo()==0){
            //申请中 审核通过 变初审通过
            iJrFactoringService.updateFactoring(jrFactoring.getId(),2,jrFactoring.getAuditMemo());
        }else if(jrFactoring.getApplyState()==1 && jrFactoring.getYesNo()==1){
            //申请中 审核不通过 变初审不通过
            iJrFactoringService.updateFactoring(jrFactoring.getId(),3,jrFactoring.getAuditMemo());
        }else if(jrFactoring.getApplyState()==2 && jrFactoring.getYesNo()==0){
            //初审通过 审核通过 变复审通过
            iJrFactoringService.updateFactoring(jrFactoring.getId(),4,jrFactoring.getAuditMemo());
        }else if(jrFactoring.getApplyState()==2 && jrFactoring.getYesNo()==1){
            //初审通过 审核不通过 变复审不通过
            iJrFactoringService.updateFactoring(jrFactoring.getId(),5,jrFactoring.getAuditMemo());
        }
        return MsgResult.succeed();
    }
}
